package com.cw.service.impl;

import com.cw.entity.SysRole;
import com.cw.entity.SysUserRole;
import com.cw.mapper.SysRoleMapper;
import com.cw.mapper.SysUserRoleMapper;
import com.cw.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色管理 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-28
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleDao;

    @Override
    public List<SysRole> getSysRole(String roleId, String roleName, String roleCode) {
        return baseMapper.getSysRole(roleId, roleName, roleCode);
    }

    /**
     * 获取角色列表通过角色名称
     * @param roleName 角色名称
     * @return java.util.List<com.kszs.entity.SysRole>
     * @author ly
     * @date 2020/10/28 10:28
     */
    @Override
    public List<SysRole> getSysRoleByRoleName(String roleName) {

        return baseMapper.getSysRoleByRoleName(roleName);
    }

    @Override
    public int insertSysRole(SysRole sysRole) {
        sysRole.setRoleId(UUID.randomUUID().toString().replace("-",""));
        return baseMapper.insertSysRole(sysRole);
    }

    @Override
    public int updateSysRole(SysRole sysRole) {

        return baseMapper.updateSysRole(sysRole);
    }

    @Override
    public int deleteSysRole(String roleId) {

        return baseMapper.deleteSysRole(roleId);
    }

    @Override
    public int deleteManySysRole(List<String> roleIds) {

        return baseMapper.deleteManySysRole(roleIds);
    }

    /**
     * 获取角色详情
     * @param id 主键
     * @return com.kszs.entity.SysRole
     * @author ly
     * @date 2020/10/21 11:46
     */
    @Override
    public SysRole getSysRole(String id) {
        return baseMapper.getSysRoleById(id);
    }

    /**
     * 获取角色主键集合通过用户主键
     * @param userId 用户主键
     * @return java.util.List<java.lang.String>
     * @author ly
     * @date 2020/10/14 11:29
     */
    @Override
    public List<String> getRolesByUserId(String userId) {
        return sysUserRoleDao.getSysUserRole(userId).stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
    }
}
